/*
Created by =BTC= Giallustio
version 0.6
Visit us at: 
http://www.blacktemplars.altervista.org/
06/03/2012
*/
//Functions
BTC_fnc_handledamage =
{
	_player = _this select 0;
	_enemy  = _this select 3;
	_damage = _this select 2;
	_part   = _this select 1;
	if (Alive _player) then
	{
		BTC_weapons = weapons _player;
		BTC_prim_weap = primaryWeapon _player;
		BTC_prim_items = primaryWeaponItems _player;
		BTC_sec_weap = secondaryWeapon _player;
		BTC_sec_items = secondaryWeaponItems _player;
		BTC_items_assigned = assignedItems _player;
		//BTC_items = items _player;
		BTC_handgun = handgunWeapon _player;
		BTC_handgun_items = handgunItems _player;
		//BTC_mags = magazines _player;
		BTC_goggles = goggles _player;
		BTC_headgear = headgear _player;
		BTC_uniform = uniform _player;
		BTC_uniform_items = uniformItems _player;
		BTC_vest = vest _player;
		BTC_vest_items = vestItems _player;
		BTC_back_pack = backpack _player;
		BTC_back_pack_items = backpackItems _player;
		BTC_back_pack_weap = getWeaponCargo (unitBackpack _player);
		BTC_weap_sel = currentWeapon _player;
		BTC_at_mag = "";BTC_r_mag = "";BTC_h_mag = "";
		if (BTC_sec_weap != "") then {player selectWeapon BTC_sec_weap;BTC_at_mag = currentMagazine player;player selectWeapon BTC_weap_sel;};
		if (BTC_handgun != "") then {player selectWeapon BTC_handgun;BTC_h_mag = currentMagazine player;player selectWeapon BTC_weap_sel;};
		if (BTC_prim_weap != "") then {player selectWeapon BTC_prim_weap;BTC_r_mag = currentMagazine player;player selectWeapon BTC_weap_sel;};
	};
	_damage
	//if (format ["%1", _player getVariable "BTC_need_revive"] == "1") then {} else {_damage};
};
BTC_fnc_PVEH =
{
	//0 - first aid - create // [0,east,pos]
	//1 - first aid - delete
	_array = _this select 1;
	_type  = _array select 0;
	_debug = true;
	if (_debug) then {diag_log format["MSO-%1 BTC_fnc_PVEH: %2", time, _this];};
	switch (true) do
	{
		case (_type == 0) : 
		{
			_side = _array select 1;
			_unit = _array select 3;
			if (_side == BTC_side) then 
			{
				_pos = _array select 2;
				_marker = createmarkerLocal [format ["FA_%1", _pos], _pos];
				format ["FA_%1", _pos] setmarkertypelocal "mil_box";
				if (typename _unit == typename objNull) then {
					if (!isnull _unit) then {
						format ["FA_%1", _pos] setMarkerTextLocal format [localize "STR_UNIT_DOWN", name _unit];
					};
				} else {
					format ["FA_%1", _pos] setMarkerTextLocal format [localize "STR_WOUNDED"];
				};
				format ["FA_%1", _pos] setmarkerColorlocal "ColorGreen";
				format ["FA_%1", _pos] setMarkerSizeLocal [0.3, 0.3];
				[_pos,_unit] spawn
				{
					_pos  = _this select 0;
					_unit = _this select 1;
					while {(!(isNull _unit) && (format ["%1", _unit getVariable "BTC_need_revive"] == "1"))} do
					{
						format ["FA_%1", _pos] setMarkerPosLocal getpos _unit;
						sleep 1;
					};
					deleteMarker format ["FA_%1", _pos];
				};
			};
		};
		case (_type == 1) : {(_array select 1) setDir 180;(_array select 1) playMoveNow "AinjPpneMstpSnonWrflDb_grab";};
		case (_type == 2) : 
		{
			private ["_injured"];
			_injured = (_array select 1);
			[_injured] spawn
			{
				_injured = _this select 0;
				_injured allowDamage false;
				WaitUntil {sleep 1; (isNull _injured) || (format ["%1", _injured getVariable "BTC_need_revive"] == "0")};
				_injured allowDamage true;
			};
		};
		case (_type == 3) :
		{
			private ["_player","_score"];
			_player = _array select 1;
			_score = _array select 2;
			_player addScore _score;
		};
	};
};
BTC_first_aid =
{
	private ["_injured","_array_item_injured","_array_item","_cond"];
	_men = nearestObjects [player, ["Man"], 2];
	if (count _men > 1) then {_injured = _men select 1;};
	if (format ["%1",_injured getVariable "BTC_need_revive"] != "1") exitWith {};
	_array_item = items player;
	_array_item_injured = items _injured;
	_cond = false;
	if (BTC_need_first_aid == 0) then {_cond = true;};
	if ((_array_item_injured find "FirstAidKit" == -1) && (BTC_need_first_aid == 1)) then {_cond = false;} else {_cond = true;};
	if (!_cond && BTC_need_first_aid == 1) then {if ((_array_item find "FirstAidKit" == -1)) then {_cond = false;} else {_cond = true;};};
	if (!_cond) exitWith {hint (localize "STR_CANT_REVIVE");};
	if (BTC_need_first_aid == 1) then {if (_array_item_injured find "FirstAidKit" == -1) then {player removeItem "FirstAidKit";};};
	player playMove "AinvPknlMstpSlayWrflDnon_medic";
	sleep 5;
	waitUntil {!Alive player || (animationState player != "AinvPknlMstpSlayWrflDnon_medic" && animationState player != "amovpercmstpsraswrfldnon_amovpknlmstpsraswrfldnon" && animationState player != "amovpknlmstpsraswrfldnon_ainvpknlmstpslaywrfldnon" && animationState player != "ainvpknlmstpslaywrfldnon_amovpknlmstpsraswrfldnon")};
	if (Alive player && Alive _injured) then
	{
		_injured setVariable ["BTC_need_revive",0,true];
		_injured playMoveNow "AinjPpneMstpSnonWrflDnon_rolltoback";
		if (BTC_score_change > 0) then {
			BTC_score_pveh = [3,player,BTC_score_change];
			publicVariable "BTC_score_pveh";
		};
	};
};
BTC_drag =
{
	private ["_injured"];
	_men = nearestObjects [player, ["Man"], 2];
	if (count _men > 1) then {_injured = _men select 1;};
	if (format ["%1",_injured getVariable "BTC_need_revive"] != "1") exitWith {};
	BTC_dragging = true;
	_injured attachTo [player, [0, 1.1, 0.092]];
	player playMoveNow "AcinPknlMstpSrasWrflDnon";
	_id = player addAction [("<t color=""#ED2744"">") + (localize "STR_RELEASE") + "</t>","base\support\modules\btc_revive\=BTC=_addAction.sqf",[[],BTC_release], 1000, true, true, "", "true"];
	_injured playMoveNow "AinjPpneMstpSnonWrflDb_grab";
	BTC_drag_pveh = [1,_injured];publicVariable "BTC_drag_pveh";
	WaitUntil {!Alive player || ((animationstate player == "acinpknlmstpsraswrfldnon") || (animationstate player == "acinpknlmwlksraswrfldb"))};
	while {!isNull player && alive player && !isNull _injured && alive _injured && format ["%1", _injured getVariable "BTC_need_revive"] == "1" && BTC_dragging} do
	{
		sleep 0.1;
	};
	detach _injured;
	if (format ["%1",_injured getVariable "BTC_need_revive"] == "1") then {_injured playMoveNow "AinjPpneMstpSnonWrflDb_release";};
	player removeAction _id;
	BTC_dragging = false;
};
BTC_release =
{
	BTC_dragging = false;
	player playMoveNow "AmovPknlMstpSrasWrflDnon";//AmovPpneMstpSrasWrflDnon
};
BTC_player_killed =
{
	private ["_type_backpack","_weapons","_magazines","_weapon_backpack","_ammo_backpack","_score","_score_array","_name","_body_marker"];
	titleText ["", "BLACK OUT"];
	_body = _this select 0;
	[_body] spawn
	{
		diag_log format["BTC_player_killed.%1: Start %2",time,_this];
		_body = _this select 0;
		_dir = getDir _body;
		_pos = getPosATL vehicle _body;
		if (BTC_active_lifes == 1) then {BTC_lifes = BTC_lifes - 1;};
		if (BTC_active_lifes == 1 && BTC_lifes == 0) exitWith BTC_out_of_lifes;
		if (BTC_lifes != 0 || BTC_active_lifes == 0) then
		{
			WaitUntil {Alive player};
			if (BTC_respawn_gear == 1) then 
			{
				_body_marker = player;
				if (BTC_pvp == 0) then {player setcaptive true;};
				//player setvehicleInit "this allowDamage false;";ProcessInitCommands;
				BTC_killed_pveh = [2,_body_marker];publicVariable "BTC_killed_pveh";player allowDamage false;
				player setVariable ["BTC_need_revive",1,true];
				player switchMove "AinjPpneMstpSnonWrflDnon";
				if ([player] call BTC_is_class_can_revive) then {player addAction [("<t color=""#ED2744"">") + (localize "STR_REVIVE") + "</t>","base\support\modules\btc_revive\=BTC=_addAction.sqf",[[],BTC_first_aid], 8, true, true, "", "[] call BTC_check_action_first_aid"];};
				player addAction [("<t color=""#ED2744"">") + (localize "STR_DRAG_BODY") + "</t>","base\support\modules\btc_revive\=BTC=_addAction.sqf",[[],BTC_drag], 8, true, true, "", "[] call BTC_check_action_drag"];
				removeAllweapons player;
				removeuniform player;
				removevest player;
				removeheadgear player;
				removegoggles player;
				removeBackPack player;
				{player removeItem _x} foreach (items player);
				{player unassignItem _x;player removeItem _x} foreach (assignedItems player);
				if (BTC_uniform != "") then {player addUniform BTC_uniform;};
				player addBackpack "B_AssaultPack_blk"; 
				if (count BTC_items_assigned > 0) then {{if (_x != "") then {player addItem _x;player assignItem _x;sleep 0.01;};} foreach BTC_items_assigned;};
				removeBackPack player;
				{if (isClass (configFile >> "cfgMagazines" >> _x)) then {player addMagazine _x;} else {player addItem _x;};sleep 0.1;} foreach BTC_uniform_items;
				if (BTC_vest != "") then {player addVest BTC_vest;};
				{if (isClass (configFile >> "cfgMagazines" >> _x)) then {player addMagazine _x;} else {player addItem _x;};sleep 0.1;} foreach BTC_vest_items;				
				if (BTC_back_pack != "") then {player addBackPack BTC_back_pack;clearAllItemsFromBackpack player;};
				{if (isClass (configFile >> "cfgMagazines" >> _x)) then {player addMagazine _x;} else {player addItem _x;};sleep 0.1;} foreach BTC_back_pack_items;
				if (BTC_goggles != "") then {player addGoggles BTC_goggles;};
				if (BTC_headgear != "") then {player addHeadgear BTC_headgear;};
				if (BTC_at_mag != "") then {player addMagazine BTC_at_mag;};
				if (BTC_r_mag != "") then {player addMagazine BTC_r_mag};
				if (BTC_h_mag != "") then {player addMagazine BTC_h_mag};
				if (count (BTC_back_pack_weap select 0) > 0) then 
				{
					for "_i" from 0 to (count (BTC_back_pack_weap select 0) - 1) do
					{
						(unitBackpack player) addweaponCargoGlobal [(BTC_back_pack_weap select 0) select _i,(BTC_back_pack_weap select 1) select _i];
					};			
				};
				//if (count BTC_mags > 0) then {{player addMagazine _x;sleep 0.01;} foreach BTC_mags;};
				if (count BTC_items_assigned > 0) then {{if (_x != "") then {player addItem _x;player assignItem _x;sleep 0.01;};} foreach BTC_items_assigned;};
				//if (count BTC_items > 0) then {{if (_x != "") then {player addItem _x;sleep 0.01;};} foreach BTC_items;};
				{if (isClass (configFile >> "cfgWeapons" >> _x)) then {player addweapon _x;};} foreach BTC_weapons;
				{player removeItemFromPrimaryWeapon _x} foreach (primaryWeaponItems player);
				if (count BTC_prim_items > 0) then {{player addPrimaryWeaponItem _x;} foreach BTC_prim_items;};
				if (count BTC_sec_items > 0) then {{player addSecondaryWeaponItem _x;} foreach BTC_sec_items;};
				if (count BTC_handgun_items > 0) then {{player addHandgunItem _x;} foreach BTC_handgun_items;};
				player selectweapon BTC_weap_sel;
			};
			//Debug
			/*
			diag_log text format ["UNIFORM %1",BTC_uniform_items];
			diag_log text format ["vest %1",BTC_vest_items];
			diag_log text format ["back_pack %1",BTC_back_pack_items];
			diag_log text format ["UNIFORM %1",uniformItems player];
			diag_log text format ["vest %1",vestItems player];
			diag_log text format ["back_pack %1",backPackItems player];
			*/
			WaitUntil {animationstate player == "ainjppnemstpsnonwrfldnon"};
			sleep 2;
			player setDir _dir;
			player setPosATL _pos;
			deletevehicle _body;
			_side = side player;
			_injured = player;
			if (BTC_injured_marker == 1) then {BTC_marker_pveh = [0,BTC_side,_pos,player];publicVariable "BTC_marker_pveh";};
			disableUserInput true;
			for [{_n = BTC_revive_time_min}, {_n != 0 && damage player > 0.2}, {_n = _n - 0.5}] do
			{
				if (BTC_active_lifes == 1) then {titleText [format ["%1: %2",localize "STR_REMAINING_LIFES",BTC_lifes], "BLACK FADED"];} else {titleText ["", "BLACK FADED"];};
				sleep 0.5;
			};
			if (BTC_black_screen == 0) then {titleText ["", "BLACK IN"];};
			disableUserInput false;
			_time = time;
			_timeout = _time + BTC_revive_time_max;
			private ["_id","_lifes"];
			if (BTC_disable_respawn == 1) then {player enableSimulation false;};
			if (BTC_black_screen == 0 && BTC_disable_respawn == 0) then {if (BTC_action_respawn == 0) then {_dlg = createDialog "BTC_respawn_button_dialog";} else {_id = player addAction [("<t color=""#ED2744"">") + (localize "STR_RESPAWN") + "</t>","base\support\modules\btc_revive\=BTC=_addAction.sqf",[[],BTC_player_respawn], 9, true, true, "", "true"];};};
			if (BTC_black_screen == 1 && BTC_disable_respawn == 0) then {_dlg = createDialog "BTC_respawn_button_dialog";};
			while {format ["%1", player getVariable "BTC_need_revive"] == "1" && time < _timeout} do
			{
				if (BTC_black_screen == 0) then {if (animationstate player != "ainjppnemstpsnonwrfldnon" && animationstate player != "AinjPpneMstpSnonWrflDb_grab") then {player switchMove "AinjPpneMstpSnonWrflDnon";};};
				if (BTC_disable_respawn == 0) then {if (BTC_black_screen == 1 || (BTC_black_screen == 0 && BTC_action_respawn == 0)) then {if (!Dialog) then {_dlg = createDialog "BTC_respawn_button_dialog";};};};
				_healer = call BTC_check_healer;
				_lifes = "";
				if (BTC_active_lifes == 1) then {_lifes = format ["%1: %2",localize "STR_REMAINING_LIFES",BTC_lifes];};
				if (BTC_black_screen == 1) then {titleText [format ["%1\n%2\n%3", round (_timeout - time),_healer,_lifes], "BLACK FADED"]} else {hintSilent format ["%1\n%2\n%3", round (_timeout - time),_healer,_lifes];};
				sleep 0.5;
			};
			closedialog 0;
			if (time > _timeout && format ["%1", player getVariable "BTC_need_revive"] == "1") then 
			{
				_respawn = [] spawn BTC_player_respawn;
			};
			if (format ["%1", player getVariable "BTC_need_revive"] == "0" && !BTC_respawn_cond) then 
			{
				if (BTC_black_screen == 1) then {titleText ["", "BLACK IN"];} else {hintSilent "";};
				if (BTC_need_first_aid == 1 && ((items player) find "FirstAidKit" != -1)) then {player removeItem "FirstAidKit";};
				player playMove "amovppnemstpsraswrfldnon";
				player playMove "";
			};
			if (BTC_black_screen == 0 && BTC_action_respawn == 1 && BTC_disable_respawn == 0) then {player removeAction _id;};
			if (BTC_pvp == 0) then {player setcaptive false;};
			if (BTC_disable_respawn == 1) then {player enableSimulation true;};
			//player setvehicleInit "this allowDamage true;";ProcessInitCommands;
			player allowDamage true;
			hintSilent "";
			diag_log format["BTC_player_killed.%1: End %2",time,_id];
		};
	};
};
BTC_check_healer =
{
	_pos = getpos player;
	_men = [];_dist = 501;_healer = objNull;_healers = [];
	_msg = localize "STR_NO_HEALER";
	_men = nearestObjects [_pos, BTC_who_can_revive, 500];
	if (count _men > 0) then
	{
		{if (Alive _x && format ["%1",_x getVariable "BTC_need_revive"] != "1" && ([_x,player] call BTC_can_revive) && isPlayer _x && side _x == BTC_side) then {_healers = _healers + [_x];};} foreach _men;
		if (count _healers > 0) then
		{
			{
				if (_x distance _pos < _dist) then {_healer = _x;_dist = _x distance _pos;};
			} foreach _healers;
			if !(isNull _healer) then {_msg = format [localize "STR_NEAR_HEALER", name _healer,round(_healer distance _pos)];};
		};
	};
	_msg
};
BTC_player_respawn =
{
	diag_log format["BTC_player_respawn.%1: Start %2",time,_this];
	BTC_respawn_cond = true;
	if (BTC_score_change > 0) then {
		BTC_score_pveh = [3,player,(0 - BTC_score_change)];publicVariable "BTC_score_pveh";
	};
	if (BTC_black_screen == 0) then {titleText ["", "BLACK OUT"];};
	player setVariable ["BTC_need_revive",0,true];
	player setPos getMarkerPos BTC_respawn_marker;
	sleep 1;
	if (BTC_MHQ_HealAction >= 0) then {
		player removeAction BTC_MHQ_HealAction;
		BTC_MHQ_HealAction = -1;
	};
	player setDamage 0;
	player switchMove "amovpercmstpslowwrfldnon";
	player switchMove "";
	if (BTC_respawn_time > 0) then
	{
		player enableSimulation false;
		player setpos [0,0,6000];
		player setVelocity [0,0,0];
		private ["_n"];
		for [{_n = BTC_respawn_time}, {_n != 0}, {_n = _n - 1}] do
		{
			private ["_msg"];
			player enableSimulation false;
			player setpos [0,0,6000];
			player setVelocity [0,0,0];
			titleText [format [localize "STR_RESPAWN_IN",_n], "BLACK FADED"];
			sleep 1;
		};
		titleText ["", "BLACK FADED"];
		player enableSimulation true;
		deTach player;
		player setVelocity [0,0,0];
		player setPos getMarkerPos BTC_respawn_marker;
		deleteVehicle _obj;
	};
	player setVariable ["BTC_need_revive",0,true];
	BTC_respawn_cond = false;
	if (BTC_black_screen == 1 && BTC_respawn_time == 0) then {
		titleText ["", "BLACK IN"];
	};
	private ["_count"];
	_count = 0;
	if (!isnil "tyr_fnc_execHandler") then {
		_count = ["BTC_RESPAWN",[player]] call tyr_fnc_execHandler;
	};
	if (_count <= 0) then {
		titleText ["", "PLAIN"];
	};
	diag_log format["BTC_player_respawn.%1: End %2",time,_count];
};
BTC_check_action_first_aid =
{
	private ["_injured","_array_item_injured"];
	_cond = false;
	_array_item = items player;
	_men = nearestObjects [vehicle player, ["Man"], 2];
	if (count _men > 1 && format ["%1", player getVariable "BTC_need_revive"] == "0") then
	{
		if (format ["%1", (_men select 1) getVariable "BTC_need_revive"] == "1" && !BTC_dragging) then {_cond = true;};
		_injured = _men select 1;
	};
	if (_cond && BTC_pvp == 1) then 
	{
		if (side (_men select 1) == BTC_side) then {_cond = true;} else {_cond = false;};
	};
	if (_cond && BTC_need_first_aid == 1) then
	{
		
		if (_array_item find "FirstAidKit" == -1) then {_cond = false;};
		_array_item_injured = items _injured;
		if (!_cond && _array_item_injured find "FirstAidKit" != -1) then {_cond = true;};
	};
	_cond
};
BTC_check_action_drag =
{
	_cond = false;
	_men = nearestObjects [vehicle player, ["Man"], 2];
	if (count _men > 1) then
	{
		if (format ["%1", (_men select 1) getVariable "BTC_need_revive"] == "1" && !BTC_dragging) then {_cond = true;};
	};
	_cond
};
BTC_is_class_can_revive =
{
	_unit    = _this select 0;
	_cond = false;
	{if (_unit isKindOf _x) then {_cond = true};} foreach BTC_who_can_revive;
	_cond
};
BTC_can_revive =
{
	_unit    = _this select 0;
	_injured = _this select 1;
	_array_item_unit    = items _unit;
	_array_item_injured = items _injured;
	_cond = false;
	{if (_unit isKindOf _x) then {_cond = true};} foreach BTC_who_can_revive;
	if (_cond && BTC_need_first_aid == 1) then
	{
		if (_array_item_unit find "FirstAidKit" == -1) then {_cond = false;};
		if (!_cond && _array_item_injured find "FirstAidKit" != -1) then {_cond = true;};
	};
	_cond
};
//Mobile
BTC_move_to_mobile =
{
	_var = _this select 0;
	_side = "";
	if (BTC_side == west) then {_side = "BTC_mobile_west";} else {_side = "BTC_mobile_east";};
	_mobile = objNull;
	{
		if ((typeName (_x getvariable _side)) == "STRING") then
		{
			if ((_x getvariable _side) == _var) then {_mobile = _x;};
		};
	} foreach vehicles;
	if (isNull _mobile) exitWith {};
	if (speed _mobile > 2) exitWith {hint localize "STR_RESPAWN_MOVING";};
	_pos = getPos _mobile;
	titleText [localize "STR_GET_READY", "BLACK OUT"];
	sleep 3;
	titleText [localize "STR_GET_READY", "BLACK FADED"];
	sleep 2;
	titleText ["", "BLACK IN"];
	_pos = [(_pos select 0) + ((random 10) - (random 10)), (_pos select 1) + ((random 10) - (random 10)), 0];
	if (count (_pos findEmptyPosition[1,10]) > 0) then { _pos = _pos findEmptyPosition[1,10]; };
	player setPos _pos;
};
BTC_mobile_marker =
{
	_var = _this select 0;
	_side = "";
	if (BTC_side == west) then {_side = "BTC_mobile_west";} else {_side = "BTC_mobile_east";};
	while {true} do
	{
		_obj = objNull;
		while {isNull _obj} do 
		{
			{
				if (format ["%1",_x getVariable _side] == _var && Alive _x) then {_obj = _x;};
			} foreach vehicles;
			sleep 1;
		};
		deleteMarkerLocal format ["%1", _var];
		_marker = createmarkerLocal [format ["%1", _var], getPos _obj];
		format ["%1", _var] setmarkertypelocal "mil_dot";
		format ["%1", _var] setMarkerTextLocal format ["%1", _var];
		format ["%1", _var] setmarkerColorlocal "ColorGreen";
		format ["%1", _var] setMarkerSizeLocal [0.5, 0.5];
		hint format [localize "STR_IS_AVAILABLE", _var];
		while {Alive _obj} do
		{
			format ["%1", _var] setMarkerPosLocal (getPos _obj);
			if (speed _obj < 2) then {format ["%1", _var] setMarkerTextLocal format [localize "STR_IS_DEPLOYED", _var];format ["%1", _var] setmarkerColorlocal "ColorGreen";} else {format ["%1", _var] setMarkerTextLocal format [localize "STR_IS_MOVING", _var];format ["%1", _var] setmarkerColorlocal "ColorBlack";};
			sleep 1;
		};
		hint format [localize "STR_HAS_BEEN_DESTROYED", _var];
		format ["%1", _var] setMarkerTextLocal format [localize "STR_IS_DESTROYED", _var];
		format ["%1", _var] setmarkerColorlocal "ColorRed";
		if (BTC_mobile_respawn == 0) exitWith {};
	};
};
BTC_mobile_check =
{
	_var = _this select 0;
	_side = "";
	if (BTC_side == west) then {_side = "BTC_mobile_west";} else {_side = "BTC_mobile_east";};
	_cond = false;
	{
		if ((typeName (_x getvariable _side)) == "STRING") then
		{
			if ((_x getvariable _side) == _var) then {_cond = true;};
		};
	} foreach vehicles;
	_cond
};
BTC_vehicle_mobile_respawn =
{
	_veh  = _this select 0;
	_var  = _this select 1;
	_set  = _this select 2;
	_type = typeOf _veh;
	_pos  = getPos _veh;
	_dir  = getDir _veh;
	waitUntil {sleep 1; !Alive _veh};
	_veh setVariable [_set,0,true];
	sleep BTC_mobile_respawn_time;
	_veh  = _type createVehicle _pos;
	_veh setDir _dir;
	_veh setVelocity [0, 0, -1];
	_veh setVariable [_set,_var,true];
	_resp = [_veh,_var,_set] spawn BTC_vehicle_mobile_respawn;
	_veh addEventHandler ["getin",{if (player == driver (_this select 0)) then{ hint Format[(localize "STR_ENTERED_MHQ"),vehicleVarName (_this select 0)];};}];
	_veh setpos _pos;
};
BTC_out_of_lifes =
{
	removeAllWeapons player;
	player enableSimulation false;
	titleText [localize "STR_NO_MORE_LIFES", "BLACK FADED"];//BLACK FADED
	sleep 1;
	while {true} do
	{
		player enableSimulation false;
		player setpos [0,0,6000];
		player setVelocity [0,0,0];
		titleText [localize "STR_NO_MORE_LIFES", "BLACK FADED"];
		sleep 1;
	};
};
BTC_fnc_MobileHQNear = 
{
	private ["_return"];
	_return = false;
	if (BTC_active_mobile == 1 && alive player) then {
		if (!_return && count BTC_vehs_mobile_west != 0) then {
			{ if ((_x distance player) <= 6) exitWith { _return = true; };
			} foreach BTC_vehs_mobile_west;
		};
		if (!_return && count BTC_vehs_mobile_east != 0) then {
			{ if ((_x distance player) <= 6) exitWith { _return = true; };
			} foreach BTC_vehs_mobile_east;
		};
	};
	_return;
};

BTC_fnc_HealAtMHQ = 
{
	player playMove "AinvPknlMstpSlayWrflDnon_medic";
	sleep 5;
	waitUntil {!Alive player || (animationState player != "AinvPknlMstpSlayWrflDnon_medic" && animationState player != "amovpercmstpsraswrfldnon_amovpknlmstpsraswrfldnon" && animationState player != "amovpknlmstpsraswrfldnon_ainvpknlmstpslaywrfldnon" && animationState player != "ainvpknlmstpslaywrfldnon_amovpknlmstpsraswrfldnon")};
	if (alive player) then {
		player setBleedingRemaining 0;
		player setDamage 0;
	};
};

BTC_fnc_AddHealAction =
{
	if (BTC_MHQ_HealAction < 0) then {
		BTC_MHQ_HealAction = player addAction [("<t color=""#ED2744"">") + (localize "STR_HEAL_AT_MHQ") + "</t>","base\support\modules\btc_revive\=BTC=_addAction.sqf",[[],BTC_fnc_HealAtMHQ], 10000, true, true, "", "alive player && (vehicle player == player) && ((isBleeding player) || (damage player) > 0)"];
	};
};

BTC_fnc_RemoveHealAction =
{
	if (BTC_MHQ_HealAction > 0) then {
		player removeAction BTC_MHQ_HealAction;
		BTC_MHQ_HealAction = -1;
	};
};

